// 导入数据库操作模块
const db = require('../db/index')

const jwt = require('jsonwebtoken')
const { search } = require('../router/perInfo')

exports.mriList = (req, res) => {
    const sql = `select * from mri`
    db.query(sql, (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '获取图谱信息成功！',
            data: results,
            total: results.length,
            pagenum: 1
        })
    })
}
exports.ctList = (req, res) => {
    const sql = `select * from ct`
    db.query(sql, (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '获取图谱信息成功！',
            data: results,
            total: results.length,
            pagenum: 1
        })
    })
}

exports.addcttp = (req, res) => {
    const sql01 = `select * from ct where ctID = ?`
    const ctInfo = req.body
    if (!ctInfo.ctID) return res.er('请输入图谱ID')
    if (!ctInfo.firPart) return res.er('请选择一级部位')
    if (!ctInfo.secPart) return res.er('请选择二级部位')
    if (!ctInfo.section) return res.er('请选择切面')
    if (!ctInfo.imgAddr) return res.er('请选择CT图谱')
    if (!ctInfo.colorImgAddr) return res.er('请选择彩绘图谱')
        // if (!ctInfo.orlist) return res.er('请选择关联的组织器官')
    db.query(sql01, ctInfo.ctID, (err, results) => {
        if (err) {
            return res.er(err)
        }
        // 判断图谱ID是否被占用
        if (results.length > 0) {
            return res.er('图谱ID已被占用')
        }
        const sql02 = `insert into ct set ?;`
        db.query(sql02, { ctID: ctInfo.ctID, firPart: ctInfo.firPart, secPart: ctInfo.secPart, section: ctInfo.section, imgAddr: ctInfo.imgAddr, colorImgAddr: ctInfo.colorImgAddr }, (err, results) => {
            if (err) {
                return res.er(err.message)
            }
            if (results.affectedRows !== 1) {
                return res.er('添加图谱失败')
            }
            res.er('添加成功', 0)
        })
    })
}

exports.addmritp = (req, res) => {
    const sql01 = `select * from mri where mriID = ?`
    const mriInfo = req.body
    if (!mriInfo.mriID) return res.er('请输入图谱ID')
    if (!mriInfo.firPart) return res.er('请选择一级部位')
    if (!mriInfo.secPart) return res.er('请选择二级部位')
    if (!mriInfo.section) return res.er('请选择切面')
    if (!mriInfo.imgAddr) return res.er('请选择MRI图谱')
    if (!mriInfo.colorImgAddr) return res.er('请选择彩绘图谱')
        // if (!ctInfo.orlist) return res.er('请选择关联的组织器官')
    db.query(sql01, mriInfo.mriID, (err, results) => {
        if (err) {
            return res.er(err)
        }
        // 判断图谱ID是否被占用
        if (results.length > 0) {
            return res.er('图谱ID已被占用')
        }
        const sql02 = `insert into mri set ?;`
        db.query(sql02, { mriID: mriInfo.mriID, firPart: mriInfo.firPart, secPart: mriInfo.secPart, section: mriInfo.section, imgAddr: mriInfo.imgAddr, colorImgAddr: mriInfo.colorImgAddr }, (err, results) => {
            if (err) {
                return res.er(err.message)
            }
            if (results.affectedRows !== 1) {
                return res.er('添加图谱失败')
            }
            res.er('添加成功', 0)
        })
    })
}

exports.delcttp = (req, res) => {
    const sql = `delete from ct where ctID = ?`
    db.query(sql, [req.params.id], (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.er(err.message)

        // SQL 语句执行成功，但是影响行数不等于 1
        if (results.affectedRows !== 1) return res.er('删除失败！')

        res.er('删除成功！', 0)
    })
}

exports.delmritp = (req, res) => {
    const sql = `delete from mri where mriID = ?`
    db.query(sql, req.params.id, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.er(err.message)

        // SQL 语句执行成功，但是影响行数不等于 1
        if (results.affectedRows !== 1) return res.er('删除失败！')

        res.er('删除成功！', 0)
    })
}

exports.editcttp = (req, res) => {
    const sql = `update ct set? where ctID=?`
    db.query(sql, [req.body, req.body.ctID], (err, results) => {
        if (err) {
            return res.er(err.message)
        }
        if (results.affectedRows !== 1) return res.er('编辑用户信息失败！')
        res.er('编辑成功', 0)
    })
}

exports.editmritp = (req, res) => {
    const sql = `update mri set? where mriID=?`
    db.query(sql, [req.body, req.body.mriID], (err, results) => {
        if (err) {
            return res.er(err.message)
        }
        if (results.affectedRows !== 1) return res.er('编辑用户信息失败！')
        res.er('编辑成功', 0)
    })
}

exports.checkct = (req, res) => {
    const sql = `select imgAddr from ct where ctID= ?`
    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '获取图谱信息成功！',
            data: results
        })
    })
}

exports.checkmri = (req, res) => {
    const sql = `select imgAddr from mri where mriID= ?`
    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '获取图谱信息成功！',
            data: results
        })
    })
}

exports.searchCT = (req, res) => {
    const sql = `select * from ct where ctID like ? or firPart like ? or secPart like ? or section like ?;`
    db.query(sql, ['%' + req.params.msg + '%', '%' + req.params.msg + '%', '%' + req.params.msg + '%', '%' + req.params.msg + '%'], (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '查询CT图谱成功！',
            data: results,
            total: results.length,
            pagenum: 1
        })
    })
}

exports.searchMRI = (req, res) => {
    const sql = `select * from mri where mriID like ? or firPart like ? or secPart like ? or section like ?;`
    db.query(sql, ['%' + req.params.msg + '%', '%' + req.params.msg + '%', '%' + req.params.msg + '%', '%' + req.params.msg + '%'], (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '查询MRI图谱成功！',
            data: results,
            total: results.length,
            pagenum: 1
        })
    })
}

exports.orlist = (req, res) => {
    const sql = `select orName from organs`
    db.query(sql, (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '获取器官成功！',
            data: results,
        })
    })
}

exports.ct_orView = (req, res) => {
    const sql = `select orName from organs right join ct_or co on organs.orID = co.orID where ctID = ? ;`
    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '获取组织器官成功',
            data: results,
        })
    })
}
exports.mri_orView = (req, res) => {
    const sql = `select orName from organs right join mri_or mo on organs.orID = mo.orID where mriID =?;`
    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.er(err.message)
        res.send({
            status: 0,
            message: '获取组织器官成功',
            data: results,
        })
    })
}